//   App Tittle: Free Note
//	Institution: Arizona State University
//	 Developers: Rodrigo Rodriguez, Andy Nguyen, Candise Orr
//	       Date: 3/27/2011

#import "ListViewController.h"
#import "BookList.h"


@implementation ListViewController
@synthesize addScreenVC;
@synthesize myDictionary;
@synthesize notes;
@synthesize notesFF;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad
{
	self.notes = [[NSMutableArray alloc] init];
	//NSLog(@"filepath: %@\n", [Utilities documentDirectoryPath]); // Uncomment to show file path
	BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[Utilities documentDirectoryPath]];
	
	if (fileExists){
		//do nothing if file exist
	}
	else {
		[Utilities writeNoteToFile:notes]; //create the file if it does not exist
	}
	
	self.navigationItem.leftBarButtonItem = self.editButtonItem;
	self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
																							target:self 
																						action:@selector(actionHappened)];
	[super viewDidLoad];
}

- (IBAction)addPressed {
	[self.navigationController popViewControllerAnimated:YES];
}

- (void)actionHappened
{
	NoteViewController *noteVC = [[NoteViewController alloc] initWithNibName:@"NoteViewController" bundle:nil];
	noteVC.ignoreDisappear = YES;
	noteVC.newNote = YES;
	[self.navigationController pushViewController:noteVC animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
	self.notes = [Utilities readNoteFromFile];
	[self.tableView reloadData];
    [super viewWillAppear:animated];
	
}


#pragma mark -
#pragma mark Table view data source

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [notes count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
	// Configure the cell.
	
	NSMutableDictionary *temp = [notes objectAtIndex:indexPath.row];
	
	cell.textLabel.text = [temp objectForKey:@"TITLE"];
	cell.detailTextLabel.text = [temp objectForKey:@"DATE"];
    return cell;
}

//Sets behavior for edit button in list view
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
											 forRowAtIndexPath:(NSIndexPath *)indexPath
{
	
	if (editingStyle == UITableViewCellEditingStyleDelete) //this deletes the note from the listView and the file
	{
		[notes removeObjectAtIndex:indexPath.row];
		[Utilities writeNoteToFile:notes];
		[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES];
	}
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	NoteViewController *noteVC = [[NoteViewController alloc] initWithNibName:@"NoteViewController" bundle:nil];
	self.notes = [Utilities readNoteFromFile];
	
	NSMutableDictionary *temp2 = [notes objectAtIndex:indexPath.row];
	noteVC.indexOfNoteRead = indexPath.row;
	noteVC.noteInView = temp2;
	noteVC.ignoreDisappear = YES;
	noteVC.newNote = NO;
	[self.navigationController pushViewController:noteVC animated:YES];
	[noteVC release];
	 
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

}

- (void)viewDidUnload {

}


- (void)dealloc {
    [super dealloc];
}


@end

